<html>
<head><meta charset="utf-8"><title>E0373 when underlying vector does outlive thread borrowing i · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/E0373.20when.20underlying.20vector.20does.20outlive.20thread.20borrowing.20i.html">E0373 when underlying vector does outlive thread borrowing i</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210640421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/E0373%20when%20underlying%20vector%20does%20outlive%20thread%20borrowing%20i/near/210640421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeff Zarnett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/E0373.20when.20underlying.20vector.20does.20outlive.20thread.20borrowing.20i.html#210640421">(Sep 19 2020 at 23:31)</a>:</h4>
<p>I'm new to Rust and trying to compile some lecture notes to help some students learn about the subject and I came across this in chapter 16 of the book: <a href="https://doc.rust-lang.org/book/ch16-01-threads.html">https://doc.rust-lang.org/book/ch16-01-threads.html</a> . I'm running the example about spawning a thread with the vector where a closure takes place with a slight modification.</p>
<p>Here's the code I'm working with:</p>
<div class="codehilite"><pre><span></span><code>use std::thread;

fn main() {
    let v = vec![1, 2, 3];

    let handle = thread::spawn(|| {
        println!(&quot;Here&#39;s a vector: {:?}&quot;, v);
    });

    handle.join().unwrap();
    println!(&quot;{:?}&quot;, v);
}
</code></pre></div>


<p>I've added the last line <code>println!("{:?}", v);</code> to the example. Without it, it is clear that <code>v</code> may not live long enough, because the closure may outlive the current function. However, I expected that by adding a usage of <code>v</code> after the <code>join()</code>, that it would mean the compiler can reason that the vector <code>v</code> must live past the <code>join()</code> and therefore outlive the thread and its closure. Still, the compiler says <code>error[E0373]: closure may outlive the current function, but it borrows </code>v<code>, which is owned by the current function</code></p>
<p>Maybe that's incorrect, though? I thought about if I should open this as an issue in Github, but seeing that this forum exists I thought maybe I'd ask here first in case I'm overlooking something. </p>
<p>I'm working with Rust in macOS using 1.39.0 + CLion environment.</p>



<a name="210640597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/E0373%20when%20underlying%20vector%20does%20outlive%20thread%20borrowing%20i/near/210640597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/E0373.20when.20underlying.20vector.20does.20outlive.20thread.20borrowing.20i.html#210640597">(Sep 19 2020 at 23:36)</a>:</h4>
<p>This stream is for working on the compiler, you may get more answers on <a href="http://users.rust-lang.org">users.rust-lang.org</a> :)</p>



<a name="210640604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/E0373%20when%20underlying%20vector%20does%20outlive%20thread%20borrowing%20i/near/210640604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeff Zarnett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/E0373.20when.20underlying.20vector.20does.20outlive.20thread.20borrowing.20i.html#210640604">(Sep 19 2020 at 23:37)</a>:</h4>
<p>Thanks for the pointer! I'll take it over there :)</p>



<a name="210641013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/E0373%20when%20underlying%20vector%20does%20outlive%20thread%20borrowing%20i/near/210641013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Smith <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/E0373.20when.20underlying.20vector.20does.20outlive.20thread.20borrowing.20i.html#210641013">(Sep 19 2020 at 23:52)</a>:</h4>
<p>You've basically got uses of v that require it to be moved. One of these is in the spawned thread, and the other is in your original thread.</p>
<p>There are a couple of ways to solve this, including using an <code>Arc</code>:</p>
<div class="codehilite"><pre><span></span><code>use std::thread;
    use std::sync::Arc;

    fn main() {
        let v = Arc::new(vec![1, 2, 3]);

        let v1 = v.clone();
        let handle = thread::spawn(move || {
            println!(&quot;Here&#39;s a vector: {:?}&quot;, v1);
        });

        handle.join().unwrap();
        println!(&quot;{:?}&quot;, v.clone());
    }
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>